package org.sdp.pearproject.service.impl;


import org.sdp.pearproject.dto.convert.*;
import org.sdp.pearproject.dao.domain.*;
import org.sdp.pearproject.dto.request.*;
import org.sdp.pearproject.dto.response.*;
import org.sdp.pearproject.dao.mapper.*;
import org.sdp.pearproject.service.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName: TaskServiceSuperImpl
 * @Description:
 * @Author: SDP
 * @Date: 2022-07-18
 * @Version: 1.0
 * Table: pear_task
 * Comment:
 * 任务表
 */
public class TaskServiceSuperImpl extends BaseServiceImpl implements TaskService {
    @Autowired
    protected TaskMapper taskMapper;

    public static Map<String, String> uniqueFieldMap = initUniqueFieldMap();

    public static Map initUniqueFieldMap() {
        HashMap map = new HashMap<>();
        map.put("task", "编号");
        return map;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer addTask(TaskAddRequest request) throws Exception {
        TaskWithBLOBs daoRequest = new TaskAddRequestConvert().convert(request);
        fixEqualCondition(request, "task", "add", "", null);
        Integer result = 0;
        try {
            result = taskMapper.insertSelective(daoRequest);
        }catch(Exception ex) {
            ex.printStackTrace();
            throw checkDBDupException(ex, uniqueFieldMap);
        }
        if (result > 0) {
            result = daoRequest.getId();
        }
        addHistory(request, "A", "task", "api", result, null);
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer updateTask(TaskUpdateRequest request) throws Exception {
        fixEqualCondition(request, "task", "update", request.getId()+"", null);
        checkInternalId(request, "task", "update", request.getId());
        addHistory(request, "U", "task", "api", request.getId(), null);

        TaskWithBLOBs daoRequest = new TaskUpdateRequestConvert().convert(request);
        TaskExample example = new TaskExample();
        example.createCriteria()
            .andIdEqualTo(request.getId())
            .andProjectCodeEqualTo(request.getProjectCode());
        if (request.getQueryOptions() != null && request.getQueryOptions().containsKey("setnull")) {
            example.setSetNull(JSONObject.toJavaObject(request.getQueryOptions().getJSONObject("setnull"), TaskExample.CriteriaString.class));
        }
        Integer result = 0;
        try {
            result = taskMapper.updateByExampleSelective(daoRequest, example);
        }catch(Exception ex) {
            ex.printStackTrace();
            throw checkDBDupException(ex, uniqueFieldMap);
        }
        if (result > 0) {
            return result;
        }
        throw createUpdateFailException();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer deleteTask(TaskDeleteRequest request) throws Exception {
        Date now = new Date();
        Integer count = 0;

        BaseRequest abstractRequest = new BaseRequest();
        copyProperties(request, abstractRequest);

        for(int index = 0; index < request.getIdList().size(); index++) {
            fixEqualCondition(request, "task", "delete", request.getIdList().get(index)+"", null);
            checkInternalId(request, "task", "delete", request.getIdList().get(index));
            addHistory(abstractRequest, "D", "task", "api", request.getIdList().get(index), null);
            taskMapper.deleteByPrimaryKey(
              request.getIdList().get(index),
              request.getProjectCodeList().get(index)
            );
            count++;
        }
        return count;
    }

    @Override
    public TaskQueryResponse queryTask(TaskQueryRequest request) throws Exception {
        TaskWithBLOBs daoResponse = taskMapper.selectByPrimaryKeyWithBLOBs(
          request.getId(),
          request.getProjectCode()
        );
        TaskQueryResponse response = new TaskQueryResponse();
        copyProperties(daoResponse, response);
        return response;
    }

    @Override
    public List<TaskQueryResponse> listTask(TaskQueryRequest request) throws Exception {
        TaskWithBLOBs daoRequest = new TaskWithBLOBs();
        copyProperties(request, daoRequest);
        TaskExample example;
        try {
            example = TaskExample.createExample(daoRequest, request.getQueryOptions());
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createFailException(ex);
        }

        List<TaskWithBLOBs> list;
        try {
            list = taskMapper.selectByExampleWithBLOBs(example);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createDatabaseFailException(ex);
        }

        List<TaskQueryResponse> responseList = new ArrayList<>();
        for(TaskWithBLOBs item : list) {
            TaskQueryResponse newItem = new TaskQueryResponse();
            copyProperties(item, newItem);
            responseList.add(newItem);
        }

        return responseList;
    }

    @Override
    public Integer countTask(TaskQueryRequest request) throws Exception {
        TaskWithBLOBs daoRequest = new TaskWithBLOBs();
        copyProperties(request, daoRequest);
        TaskExample example;
        try {
            example = TaskExample.createExample(daoRequest, request.getQueryOptions());
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createFailException(ex);
        }

        Integer count;
        try {
            count = taskMapper.countByExample(example);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createDatabaseFailException(ex);
        }

        return count;
    }

    @Override
    public BaseStatResponse statTask(TaskQueryRequest request) throws Exception {
        throw createFailException("not implement");
    }

}
